package su2010.puz.extractors;

import su2010.puz.Extractor;
import su2010.puz.ImageHelper;
import su2010.puz.impl.Histogram;
import su2010.puz.util.ImageCls;

public class HistogramExtractor implements Extractor {

	private Histogram histogram;
	private int color_mode;
	private int numOutput = 8;
	
	public HistogramExtractor() {
		color_mode = ImageHelper.RED;
	}
	
	public void setColorMode(int color) {
		this.color_mode = color;
		if (color_mode == ImageHelper.VALUE || color_mode == ImageHelper.SATURATION)
			numOutput = 5;
	}
	
	public void setImage(ImageCls image) {
		histogram = image.getHistogram();
	}
	
	@Override
	public int getNumOutput() {
		return numOutput;
	}
	
	@Override
	public float[] extract() {
		float[] features = new float[numOutput];
		int[] hist = histogram.getHistogram(color_mode);
		int interval;
		if( color_mode == ImageHelper.RED ||
			color_mode == ImageHelper.GREEN ||
			color_mode == ImageHelper.BLUE ||
			color_mode == ImageHelper.LUMINANCE)
		{
			interval = 32;
			for(int i = 0;i<8;i++){
				float sum = 0;
				for(int j =0; j<interval;j++){
					sum += hist[i*interval+j];
				}
				features[i] = sum / (float) interval / histogram.getMaxFrequency(color_mode);
			}
		}
		else if (color_mode == ImageHelper.HUE){
			interval = 45;
			for(int i = 0;i<8;i++){
				float sum = 0;
				for(int j =0; j<interval;j++){
					sum += hist[i*interval+j];
				}
				features[i] = sum / (float) interval / histogram.getMaxFrequency(color_mode);
			}
		}
		else {
			for(int i = 0;i<5;i++){
				interval = 20;
				float sum = 0;
				for(int j =0; j<interval;j++){
					sum += hist[i*interval+j];
				}
				features[i] = sum / (float) interval / histogram.getMaxFrequency(color_mode);
			}
		}
		return features;
	}

	@Override
	public String getName() {
		return ImageHelper.getColorName(color_mode)+" Historgram ";
	}

}
